import Taro, {request} from "@tarojs/taro";
import baseAPI from "./baseAPI";
import {modelToast} from "../utils";

const showLoading = Taro.showLoading;
const hideLoading = Taro.hideLoading;

export default function http({url, header = {}, method, params = {}}) {
  // showLoading({
  //   title: '加载中',
  // }).then(_=>{})

  showLoading({
    title: '加载中',
  })

  return new Promise((resolve, reject) => {
    request({
      url: baseAPI + url,
      method,
      timeout: 6000,
      header: {...header},
      data: params,
    }).then(response => {
      const {data} = response;
      const {Code, Message, Result} = data;
      if (Code === 1000) {
        hideLoading()
        resolve(Result);
      } else {
        hideLoading()
        return modelToast({title: Message});
      }
    }).catch(err => {
      hideLoading()
      return modelToast({title: err.message});
    })
  })
}
